package com.mdd.common.util.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.utils.sm2.SM2Util;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.mdd.common.util.Base64Util;
import com.mdd.common.util.ToolUtils;

/**
 * 极光推送
 * Content-Type: application/json
 */
public class JPushUtil {

    private static final String AppKey = "9eeca031c8119a32db467d10";
    private static final String MasterSecret = "974e47f620a2a6ee9bdcb13f";

//    /**
//     * 极光推送签名
//     */
//    public static String autoGenerateSign() {
//        StringBuffer buffer = new StringBuffer().append(AppKey).append(":").append(MasterSecret);
//        String encode = Base64Util.encode(buffer.toString().getBytes());
//
//        SM2Util.encrypt(buffer.toString(),BASE64_PUBLIC_KEY);
//        return encode;
//    }


    public static void main(String[] args) {
        JPushClient jpushClient = new JPushClient(MasterSecret, AppKey);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.alert("测试消息"))
                .build();
        try {
            PushResult result = jpushClient.sendPush(payload);
            if (result.statusCode == 200) {
                System.out.println("推送成功");
            }
        } catch (APIConnectionException e) {
            throw new RuntimeException(e);
        } catch (APIRequestException e) {
            System.err.println("HTTP Status: " + e.getStatus());
            System.err.println("Error Code: " + e.getErrorCode());
            System.err.println("Error Message: " + e.getErrorMessage());
            throw new RuntimeException(e);
        }

    }
}
